'use strict';

const Service = require('egg').Service;
const axios = require('axios');

class HomeService extends Service {
  async types() {
    const {
      app,
    } = this.ctx;
    const url = `${app.config.jdUrl}jisuapi/channel?appkey=${app.config.appKey}`;
    let result;
    try {
      result = await axios.get(url);
    } catch (err) {
      result = {};
    }
    return result.data;
  }
  async recom({
    channel = '头条',
    num = 10,
    start = 1,
  }) {
    const {
      app,
    } = this.ctx;
    let result;
    channel = encodeURI(channel);
    const url = `${app.config.jdUrl}jisuapi/get?channel=${channel}&num=${Number(num)}&start=${Number(start) - 1}&appkey=${app.config.appKey}`;
    try {
      result = await axios.get(url);
    } catch (err) {

    }
    return result.data;
  }
}

module.exports = HomeService;
